package com.wflow.config;


import cn.dev33.satoken.exception.NotLoginException;
import com.wflow.exception.BusinessException;
import com.wflow.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

/**
 * @author : willian fu
 * @date : 2022/6/27
 */
@Slf4j
@Component
@RestControllerAdvice         //extends ResponseEntityExceptionHandler xubz xiu
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
    @ExceptionHandler(BusinessException.class)
    public ResponseEntity<String> businessExceptionHandler(BusinessException e){
        log.error("BusinessException：", e);
        return R.serverError(e.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public ResponseEntity<String> LoginExceptionHandler(NotLoginException e){
        log.error("NotLoginException：", e);
        return R.unAuthorized("未登录或登录Token已过期，请点击头像切换人员");
    }

    @ExceptionHandler(Exception.class)
    public ResponseEntity<String> runtimeExceptionHandler(Exception e){
        log.error("RuntimeException：", e);
        return R.serverError(e.getMessage());
    }
}
